class CardComponent extends eui.Component{
	protected base:eui.Group
	/**卡名文本 */
	protected txtName:eui.Label
	/**罕贵图标 */
	protected iconRare:eui.Image
	/**卡图 */
	protected imgCard:eui.Image
	/**是否小图 */
	private _isThumb:boolean
	/**是否公开 */
	private _isFront:boolean
	/**卡面state */
	private _frontState:string
	private _mask:egret.Sprite
	private _card:BaseCard
	public constructor(card:BaseCard=null, touchEnabled:boolean=true) {
		super()
		this.skinName = 'resource/eui_skins/scene/card.exml'
		this.once(eui.UIEvent.CREATION_COMPLETE, this.onComplete, this)
		this._card = card
		this._isFront = true
		this.touchEnabled = touchEnabled
		this.touchChildren = false
	}

	/**更换卡面 */
	public set card(c:BaseCard){
		this._card = c
		this.onComplete(null)
	}
	public get card(){
		return this._card
	}

	protected onComplete(e:any){
		const t = this
		if(!t._card) {
			throw new Error("Empty card data!")
		}
		const data = t._card.oriData
		let state = ''
		switch(data.cty){
			case CardType.WEAPON:
				state = 'weapon'
				t.initWeaponCard(data)
				break
			case CardType.SET_SHIELD:
				state = 'setshield'
				t.initSetShielCard(data)
				break
			case CardType.ACT_SHIELD:
				state = 'actshield'
				t.initActShielCard(data)
				break
			case CardType.SKILL:
				state = 'skill'
				t.initSkillCard(data)
				break
		}
		t._frontState = state
		t.currentState = t.thumbState + t.cardState
		t._mask = MaskSprite.create(t)
		t.setCardName(data.name)
		t.setRareIcon(data.rare)
		t.setCardImage(data.cty, data.img)
		
	}

	/**翻牌效果 */
	public async flip(zoom:number=1.1, stepDelay:number=120){
		const t = this
		t._isFront = !t._isFront
		let afterState = t.thumbState + t.cardState
		const halfWidth = t.width/2
		// t.anchorOffsetX
		return new Promise((resolve, reject)=>{
			egret.Tween.get(t)
			.set({
				anchorOffsetX:halfWidth,
				x:halfWidth
			})
			.to({
				scaleX:zoom,
				scaleY:zoom
			}, stepDelay)
			.to({
				scaleX:0
			}, stepDelay)
			.set({
				currentState: afterState
			})
			.to({
				scaleX:zoom
			}, stepDelay)
			.to({
				scaleX:1,
				scaleY:1
			}, stepDelay)
			.call(resolve)
		})
	}

	/**加入手牌效果 */
	public async added2Hand(){
		const t = this
		const oriY = t.base.y
		return new Promise((resolve, reject)=>{
			egret.Tween.get(t.base)
			.set({
				scaleX:1,
				alpha:0,
				y:oriY - 50
			})
			.to({
				alpha:1,
				y:oriY
			}, 250)
			.call(resolve)
		})
	}

	/**打出手牌效果  */
	public async removeFromHand(){
		const t = this
		if(!t.parent) return 
		return new Promise((resolve, reject)=>{
			egret.Tween.get(t)
			.set({
				alpha:0
			})
			.to({
				scaleX:0
			}, 100)
			.call(resolve)
			.set({
				scaleX:1
			})
		})	
	}

	/**从场上移除效果 */
	public async remove(){
		return new Promise((resolve, reject)=>{
			egret.Tween.get(this)
			.to({
				x:2000
			}, 200)
			.call(resolve)
		})
	}

	/**变成小/大图 */
	public set isThumb(v:boolean){
		const t = this
		const l = t._card.name.length
		t._isThumb = v 
		t.currentState = t.thumbState + t.cardState
		t.setCardName(t._card.name)
	}

	/**变成正/背面 */
	public set isFront(v:boolean){
		const t = this
		t._isFront = v
		t.currentState = t.thumbState + t.cardState
		t.validateNow()
	}

	public get isFront(){
		return this._isFront
	}

	protected setCardName(n:string){
		const t = this
		const l = n.length
		t.txtName.text = n
		if(t._isThumb){
			if(l>=10){
				t.txtName.size = 22
			}else if(l > 6){
				t.txtName.size = 30
			}else{
				t.txtName.size = 34
			}
		}else{
			if(l>=10){
				t.txtName.size = 36
			}else if(l > 6){
				t.txtName.size = 42
			}else{
				t.txtName.size = 45
			}
		}
	}

	/**设置罕贵图标 */
	protected setRareIcon(r:RareType){
		let iconUrl:string = ''
		switch(r){
			case RareType.N:
				iconUrl = 'icons_json#ncard'
				break
			case RareType.R:
				iconUrl = 'icons_json#rcard'
				break
			case RareType.SR:
				iconUrl = 'icons_json#srcard'
				break
			case RareType.UR:
				iconUrl = 'icons_json#urcard'
				break
		}
		this.iconRare.source = iconUrl
	}
	
	/**设置卡图 */
	protected setCardImage(ct:CardType ,url:string=''){
		if(url.length == 0){
			switch(ct){
				case CardType.WEAPON:
					url = "icons_json#weapon"
					break
				case CardType.ACT_SHIELD:
					url = "icons_json#actshield"
					break
				case CardType.SET_SHIELD:
					url = "icons_json#setshiled"
					break
				case CardType.SKILL:
					url = "icons_json#skill"
					break
			}
		}
		this.imgCard.source = url
	}

	//武器卡
	protected txtDmgType:eui.Label
	protected iconDmgType:eui.Image
	protected txtRange:eui.Label
	protected txtAtkTime:eui.Label
	protected initWeaponCard(data:ICardData){
		const t = this
		if(!data.wp) throw new Error("Illegal card data!")
		t.txtDmgType.text = Type2Str.WeaponType2Str(data.wp.ty)
		t.txtRange.text = `${data.wp.range}`
		t.txtAtkTime.text = `${data.wp.time}`
		t.iconDmgType.source = t.getWeaponTypeIcon(data.wp.ty)
	}
	public renewWeaponTime(v:number){
		this.txtAtkTime.text = `${v}`
	}

	//护盾卡
	protected txtDefType0:eui.Label
	protected iconDefType0:eui.Image
	protected rectDef0:eui.Rect
	protected txtDefType1:eui.Label
	protected iconDefType1:eui.Image
	protected rectDef1:eui.Rect
	protected txtDefType2:eui.Label
	protected iconDefType2:eui.Image
	protected rectDef2:eui.Rect
	protected initActShielCard(data:ICardData){
		const t = this
		if(!data.dp) throw new Error("Illegal card data!")

		for(let i=0; i<3; i++){
			if(data.dp[i] == WeaponType.NULL){
				t[`rectDef${i}`].fillAlpha = 0
				continue
			}
			t[`txtDefType${i}`].text = Type2Str.WeaponType2Str(data.dp[i])
			t[`iconDefType${i}`].source = t.getWeaponTypeIcon(data.dp[i])
			t[`rectDef${i}`].fillAlpha = 1
		}
	}

	//设置型护盾卡
	protected txtDefTime:eui.Label
	protected txtOp:eui.Label
	protected unit:string
	protected initSetShielCard(data:ICardData){
		const t = this
		if(!data.dt) throw new Error("Illegal card data!")
		t.unit = Type2Str.UseType2Str(data.dt[1])
		t.txtDefTime.text = `${data.dt[0]} ${t.unit}`
		t.txtOp.visible = data.op
	}
	public renewShieldTime(v:number){
		this.txtDefTime.text = `${v} ${this.unit}`
	}


	//战术卡
	/**效果描述文本 */
	protected txtSkillEffect:eui.Label
	protected initSkillCard(data:ICardData){
		const t = this
		if(!data.des) throw new Error("Illegal card data!")
		t.txtSkillEffect.text = data.des
	}

	private getWeaponTypeIcon(ty:WeaponType){
		let iconUrl = ''
		switch(ty){
			case WeaponType.SOLID:
				iconUrl = 'icons_json#solid'
				break
			case WeaponType.RASER:
				iconUrl = 'icons_json#raser'
				break
			case WeaponType.GRAVITY:
				iconUrl = 'icons_json#gravity'
				break
			case WeaponType.MENTAL:
				iconUrl = 'icons_json#mental'
				break
			case WeaponType.SPEC:
				iconUrl = 'icons_json#spec'
				break
		}
		return iconUrl
	}

	private get thumbState(){
		if(this._isThumb){
			return 'thumb_'
		}else{
			return ''
		}
	}
	private get cardState(){
		if(this._isFront){
			return this._frontState
		}else{
			return 'back'
		}
	}

	public set scaledWidth(w:number){
		const s = w/this.width
		this.scaleX = s
	}

	public set scaledHeight(h:number){
		const s = h/this.height
		this.scaleY = s
	}

	public set dragged(v:boolean){
		//this.mask = v?this._mask:null
		this.alpha = v?0:1
	}
}